package com.csdj.t309springboot.utils;
import com.csdj.t309springboot.controller.WeiXinController;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author 宇众不同
 * @datetime 2022-02-10
 */
public class ParameterNameUtils {
//    public static void main(String[] args) throws Exception {
//        Class<InterfaceTestingController> clazz = InterfaceTestingController.class;
//        Method method = clazz.getDeclaredMethod("test1", String.class, String.class, String.class, String.class, String.class, String.class);
//        String[] parameterNames = ParameterNameUtils.getMethodParameterNamesByAnnotation(method);
//        System.out.println(Arrays.toString(parameterNames));
//    }

    public static String[] order(){
        Class<WeiXinController> clazz = WeiXinController.class;
        Method method = null;
        try {
            method = clazz.getDeclaredMethod("callback", String.class, String.class, String.class, String.class, String.class, String.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        String[] parameterNames = ParameterNameUtils.getMethodParameterNamesByAnnotation(method);
        Arrays.sort(parameterNames);
        return parameterNames;
    }
    /**
     * 获取指定方法的参数名
     * @param method 要获取参数名的方法
     * @return 按参数顺序排列的参数名列表
     */
    public static String[] getMethodParameterNamesByAnnotation(Method method) {
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        if (parameterAnnotations == null || parameterAnnotations.length == 0) {
            return null;
        }
        String[] parameterNames = new String[parameterAnnotations.length];
        int i = 0;
        for (Annotation[] parameterAnnotation : parameterAnnotations) {
            for (Annotation annotation : parameterAnnotation) {
                if (annotation instanceof Param) {
                    Param param = (Param) annotation;
                    parameterNames[i++] = param.value();
                }
            }
        }
        return parameterNames;
    }

}